package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.bizimpl.upmp.service.IUpmpMenuService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpMenuVO;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.UserCommonFun;
import cc.rengu.redp.bizimpl.upmp.vo.UserCommonFunVO;
import cc.rengu.redp.bizimpl.upmp.service.IUserCommonFunService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author guohaocong
 */
@Slf4j
@RestController
@Api(tags = "用户常用功能菜单配置管理管理")
@RequestMapping("/admin/userCommonFun")
public class UserCommonFunController {

    @Autowired
    private IUserCommonFunService userCommonFunService;
    @Autowired
    private IUpmpMenuService upmpMenuService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;



    @Log("系统管理:用户常用功能菜单配置表管理")
    @GetMapping("/menusData")
    public Map<String, Object> menusData(UpmpMenuVO menu) throws RedpException {
        return this.upmpMenuService.findMenusNoCommon(menu);
    }

    @Log("系统管理:用户常用功能")
    @GetMapping("/commonData")
    public List<UserCommonFunVO> commonData() throws RedpException {
        //获取当前登录人信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String instId = user.getInstId();
        String userId = user.getId();
        return this.userCommonFunService.findCommonMenus(instId, userId);
    }

    @Log("添加用户常用功能菜单")
    @PostMapping("/addCommonMenus")
    public void addCommonMenus(String menuId) {
        //获取当前登录人信息
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        UserCommonFun entity = new UserCommonFun();
        entity.setInstId(user.getInstId());//创建机构
        entity.setUserId(user.getId());
        entity.setFunStatus("01");
        entity.setOprId(user.getId());//创建人
        entity.setCreateTime(DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));//创建时间戳
        entity.setCrtOrgId(user.getOrgCode());//创建机构
        entity.setMenuId(menuId);
        this.userCommonFunService.save(entity);
    }

    @Log("删除用户常用功能菜单")
    @DeleteMapping("/deleteCommonMenus")
    public void deleteCommonMenus(String id) {
        this.userCommonFunService.deleteMenuById(id);
    }
}
